//
//  ADView.m
//  XDEducation
//
//  Created by 星道 on 17/5/17.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "ADView.h"
#import "SDCycleScrollView.h"
#import "XDLinkPage.h"
@implementation ADView

+ (void)initLaunchAds{
    
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"IsFirst"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    BOOL IsFirst = [[NSUserDefaults standardUserDefaults] boolForKey:@"IsFirst"];
    
    /*如果是第一次启动则不显示广告页*/
    if (IsFirst) return;
    
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]stringByAppendingPathComponent:@"path1.txt"];
    NSArray *Arr = [NSArray arrayWithContentsOfFile:path];
  
    

    if (Arr.count >0)
    {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self ShowAd:Arr];
        });
    }
    else
    {

        [self ShowNetAd];
    }
    
        
    
}

+ (void)ShowAd:(NSArray *)Arr
{
    [self SetScrol:Arr];
}




+ (void)ShowNetAd
{
    
    
    // 网络加载 --- 创建带标题的图片轮播器
    [XDLinkPage GetImageURlComplete:^(NSArray *Arr) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
        [self SetScrol:Arr];
        });

    } Fail:^{
        
    }];
}


+ (void)SetScrol:(NSArray *)Arr
{
    SDCycleScrollView *cycleScrollView2 = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) delegate:nil placeholderImage:[UIImage imageNamed:@"XDBG"]];
    cycleScrollView2.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    cycleScrollView2.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    cycleScrollView2.hidesForSinglePage = @"XDBG";
    cycleScrollView2.userInteractionEnabled = NO;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:cycleScrollView2];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ScreenW-38, 28, 50, 30)];
    label.text = @"5";
    label.textColor = UIColor(150, 150, 150);
    label.font = [UIFont systemFontOfSize:13];
    [cycleScrollView2 addSubview:label];
    NSInteger index = 6;
    
    __block typeof(NSInteger)weakIndex = index;
    [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        
        weakIndex = weakIndex - 1;
        dispatch_async(dispatch_get_main_queue(), ^{
            label.text = [@(weakIndex) stringValue];
        });
        
        if (weakIndex == 0)
        {
            [timer invalidate];
            [UIView animateWithDuration:0.5 animations:^{
                
                cycleScrollView2.alpha= 0;
            } completion:^(BOOL finished) {
                [cycleScrollView2 removeFromSuperview];
            }];
        }
    }];
    //         --- 模拟加载延迟
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        cycleScrollView2.imageURLStringsGroup = Arr;
//    });
}

@end
